/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ekuma.epos.datalogic.define.dao;

import java.util.Date;
import java.util.List;

import com.openbravo.data.loader.TableDBSentenceBuilder;
import com.openbravo.data.loader.query.QBFParameters;
import e.odbo.DB;
import e.odbo.data.dao.BaseDAO;
import com.openbravo.bean.AttributeSet;
import com.openbravo.bean.Product;
import com.openbravo.bean.ProductPrefPrice;
import com.openbravo.bean.StockDiary;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.serialize.DataParams;
import com.openbravo.data.loader.serialize.DataRead;
import com.openbravo.data.loader.serialize.DataWrite;
import com.openbravo.data.loader.serialize.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.sentence.PreparedSentence;
import com.openbravo.data.loader.sqlbuilder.QBFBuilder;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteBasic;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteParams;
import com.openbravo.data.loader.serialize.serializer.write.SerializerWriteString;
import com.openbravo.data.loader.sentence.StaticSentence;
import com.openbravo.data.loader.table.TableDefinition;
import com.openbravo.data.model.Field;
import com.openbravo.format.Formats;
import com.openbravo.pos.bean.ProductExt;

/**
 *
 * @author Administrator
 */
public class ProductPrefPriceDAO extends BaseDAO<ProductPrefPrice> {

    public ProductPrefPriceDAO(I_Session s) {
        super(s);
    }

    @Override
    public TableDefinition getTable() {
        return new TableDefinition(
                "PRODUCTPREFPRICE",
                new Field[]{
                    new Field("ID", Datas.STRING, Formats.STRING),
                    new Field("ATTRIBUTESET_ID", Datas.STRING, Formats.STRING, AttributeSet.class),
                    new Field("ISCUSTOMERPREF", Datas.BOOLEAN, Formats.BOOLEAN),
                    new Field("PREFTYPE", Datas.STRING, Formats.STRING),
                    new Field("STORENUM", Datas.DOUBLE, Formats.PERCENT),
                    
                    new Field("SELLEDNUM", Datas.DOUBLE, Formats.PERCENT),
                    new Field("MINNUM", Datas.DOUBLE, Formats.PERCENT),
                    new Field("STARTDATE", Datas.TIMESTAMP, Formats.TIMESTAMP),
                    new Field("STOPDATE", Datas.TIMESTAMP, Formats.TIMESTAMP),
                    new Field("PREFMODETYPE", Datas.STRING, Formats.STRING),
                    
                    new Field("PREFRATE", Datas.DOUBLE, Formats.PERCENT),
                    new Field("PREFPRICE", Datas.DOUBLE, Formats.PERCENT),
                    new Field("LARGESSID", Datas.STRING, Formats.STRING),
                    new Field("LARGESSATTRIBUTESET_ID", Datas.STRING, Formats.STRING),
                    new Field("LARGESSNUM", Datas.DOUBLE, Formats.PERCENT),
                    
                    new Field("PRODUCT", Datas.STRING, Formats.STRING,Product.class),
                    new Field("PREFBUYPRICE", Datas.DOUBLE, Formats.PERCENT),
                    new Field("NAME", Datas.STRING, Formats.STRING),
                    new Field("CURDATE", Datas.TIMESTAMP, Formats.TIMESTAMP)
                },
                new int[]{0});
    }

    @Override
    public void writeInsertValues(DataWrite dp, ProductPrefPrice obj) throws BasicException {
        dp.setString(1, obj.getId());
        dp.setString(2, obj.getAttributesetid());
        dp.setBoolean(3, obj.isIsCustomerPref());
        dp.setString(4, obj.getPrefType());
        dp.setDouble(5, obj.getStoreNum());
        
        dp.setDouble(6, obj.getSelledNum());
        dp.setDouble(7, obj.getMinNum());
        dp.setTimestamp(8, obj.getM_dStartDate());
        dp.setTimestamp(9, obj.getM_dStopDate());  
        dp.setString(10, obj.getPrefModeType());
        
        dp.setDouble(11, obj.getPrefRate());
        dp.setDouble(12, obj.getPrefPrice());
        dp.setString(13, obj.getM_sLargessId());
        dp.setString(14, obj.getLargessAttributesetid());
        dp.setDouble(15, obj.getLargessNum());
        
        dp.setString(16,obj.getM_sId());
        dp.setDouble(17,obj.getPrefBuyPrice());
        dp.setString(18,obj.getP_name());
        dp.setTimestamp(19,obj.getCurDate());
    }

    public ProductPrefPrice readValues(DataRead dr,ProductPrefPrice p) throws BasicException {
        Double value = null;
        Date time = null;
        if(p==null)
        	p = new ProductPrefPrice();
        p.setId(dr.getString(1));
        p.setAttributesetid(dr.getString(2));
        p.setIsCustomerPref(dr.getBoolean(3).booleanValue());
        p.setPrefType(dr.getString(4));
        value = dr.getDouble(5);
        if (value != null) {
            p.setStoreNum(value);
        }
        value = dr.getDouble(6);
        if (value != null) {
            p.setSelledNum(value);
        }
        value = dr.getDouble(7);
        if (value != null) {
            p.setMinNum(value);
        }
        time = dr.getTimestamp(8);
        if (time != null) {
            p.setM_dStartDate(time);
        }
        time = dr.getTimestamp(9);
        if (time != null) {
            p.setM_dStopDate(time);
        }
        p.setPrefModeType(dr.getString(10));
        value = dr.getDouble(11);
        if (value != null) {
            p.setPrefRate(value);
        }
        value = dr.getDouble(12);
        if (value != null) {
            p.setPrefPrice(value);
        }
        p.setM_sLargessId(dr.getString(13));
        p.setLargessAttributesetid(dr.getString(14));
        value = dr.getDouble(15);
        if (value != null) {
            p.setLargessNum(value);
        }
        
        p.setM_sId(dr.getString(16));
        p.setPrefBuyPrice(dr.getDouble(17));
        p.setP_name(dr.getString(18)); 
        p.setCurDate(dr.getTimestamp(19));
        return p;
    }

    public ProductPrefPrice getProductPrefPriceByProductID(String id) throws BasicException {
        return (ProductPrefPrice) new PreparedSentence(s, ((TableDBSentenceBuilder)getTableBuilder()).getListSQL(getTable())+" WHERE PRODUCT = ?", SerializerWriteString.INSTANCE, this).find(id);
    }

    @Override
    public Class getSuportClass() {
        return ProductPrefPrice.class;
    }


	public List<ProductPrefPrice> list(Object filter) throws BasicException {
		return new StaticSentence(s, new QBFBuilder(DB.getListSQL(this,"PP")+", PRODUCTS P WHERE PP.PRODUCT=P.ID AND ?(QBF_FILTER)", new String[]{"PP.CURDATE","PP.CURDATE","PP.PRODUCT"}), new SerializerWriteBasic(Datas.OBJECT, Datas.TIMESTAMP,Datas.OBJECT, Datas.TIMESTAMP,Datas.OBJECT, Datas.STRING), this).list(filter);
	}
    
    public void updateSellNum(final String id,final StockDiary diary)throws BasicException {
    	 new PreparedSentence(
					s,
					"UPDATE PRODUCTPREFPRICE SET SELLEDNUM = (SELLEDNUM + ?) WHERE ID = ? ",
					SerializerWriteParams.INSTANCE).exec(new DataParams() {
				@Override
				public void writeValues() throws BasicException {
					setDouble(1,diary.getMultiply());
					setString(2, id);
				}
			});
    }

    @Override
    public List<ProductPrefPrice> query(QBFParameters filter) throws BasicException {
        return new StaticSentence(s, new QBFBuilder(DB.getListSQL(this,"PP")+", PRODUCTS P WHERE PP.PRODUCT=P.ID AND ?(QBF_FILTER)"),null, this).list(filter);

    }
}
